import {mongo} from "#nuxt-mongodb"
import {ObjectId} from "mongodb"
interface userMesInter {
    id: string,
    name: string,
    account: string,
    password: string,
}

export default defineEventHandler(async(event:any)=>{
    const {id,account,name,password}:userMesInter = await readBody(event)
    const idObj = new ObjectId(id)
    
    //new ObjectId('66c09a74e66b97683494181d')
    // console.log(idObj)

    const findUserByID =await mongo.db().collection('userMes').findOne({id:idObj})
    /*findUserByID: { _id: new ObjectId('66d9c29bc4eadbe8439396e0'),
        id: new ObjectId('66c09a74e66b97683494181d'),
        name: 'admin',
        isVip: false,
        loveAnime: [] }
     */
    // console.log('findUserByID:',findUserByID)
    if(findUserByID?._id != null){
        return {
            status: 200,
            data: findUserByID,
            message: '用户已存在,没有进行自动创建'
        }
    }
    
    const result = await mongo.db().collection('userMes').insertOne({id:idObj,name:account,isVip:false,loveAnime:[]});
    // console.log(result)
    return {
        status: 200,
        data: result,
        message: "用户数据自动创建成功"
    }
})